package www.dianbaozu.com.ui.my

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.netease.nim.uikit.api.NIMHelper
import com.netease.nim.uikit.xpopup.CreateXPopupHelper
import com.netease.nim.uikit.xpopup.PopupViewEntity
import com.orhanobut.logger.Logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentAccountSettingsBinding
import www.wen.com.common.lib.base.fragment.BaseVMFragment
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.extension.glide.ImageLoader
import www.wen.com.common.lib.extension.nav
import www.wen.com.common.lib.extension.resetStartDestination
import www.wen.com.common.lib.extension.toast
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.GlideCacheUtil
import www.wen.com.common.lib.util.SpUtil
import java.text.DecimalFormat

/**
 * @author WEN
 * @Description: 账号设置
 * @date 2020//25 17:48
 */
class AccountSettingsFragment : BaseVMFragment<FragmentAccountSettingsBinding>(), View.OnClickListener {

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(requireActivity(), "账号设置", action = ::titleAction)

    override fun getLayoutId(): Int = R.layout.fragment_account_settings

    override fun lazyInitView() {
        dataBinding.run {
            tvBirthday.text = SpUtil.decodeString(KeyHelper.MMKV_MOBILE)
            val ss = upCacheSize()
            Logger.i("缓存大小 = ${GlideCacheUtil.getInstance().getCacheSize(requireActivity())},ss = ${ss}")

        }

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.llChangePassword -> {
                nav(R.id.action_accountSettingsFragment_to_forgetPasswordFragment, Bundle().apply {
                    putBoolean(BUNDLE_DATA, true)
                })
            }
            R.id.llChangeMobilePhoneNumber -> {
                nav(R.id.action_accountSettingsFragment_to_modifyPhoneFragment)
            }
            R.id.llClearCache -> {
                showClearDialog()
            }
            R.id.llAboutTelegramGroup -> {
                nav(R.id.action_accountSettingsFragment_to_aboutUsFragment)
            }
            R.id.llAccountSecurity -> {
                nav(R.id.action_accountSettingsFragment_to_cancellationFragment)
            }
            R.id.loginOut -> {
                CreateXPopupHelper.createShowStandardAlertPopupView(PopupViewEntity().apply {
                    context = requireActivity()
                    standardAlertPopupViewTitle = getString(R.string.com_logout)
                    isShow = true
                    standardAlertPopupViewCancelContent = "取消"
                    standardAlertPopupViewConfirmContent = "确认"
                    standardAlertPopupViewContent = getString(R.string.com_receive_messages_no_longer)
                    confirmFunction = {
                        NIMHelper.logout()
                        requireActivity().resetStartDestination(R.navigation.navigation, R.id.loginFragment)
                        SpUtil.clearAll()
                    }
                })
            }
        }
    }

    private fun showClearDialog() {
        CreateXPopupHelper.createShowStandardAlertPopupView(PopupViewEntity().apply {
            context = requireActivity()
            standardAlertPopupViewTitle = getString(R.string.com_clear_cache)
            isShow = true
            standardAlertPopupViewCancelContent = "取消"
            standardAlertPopupViewConfirmContent = "确认"
            standardAlertPopupViewContent = "是否清除${upCacheSize()}缓存?"
            confirmFunction = {
                lifecycleScope.launch {
                    withContext(Dispatchers.IO) {
                        ImageLoader.getDefault().clearDiskCache(requireContext())
                    }
                    upCacheSize()
                    toast("清除缓存成功")
                }
            }
        })
    }

    private fun upCacheSize(): String {
        val size = roundingOffNumbers(ImageLoader.getDefault().getDiskCacheSize(requireContext()) / 1024/1024.toFloat()).toString()
        (size + "M").also {
            dataBinding.tvArea.text = it
            return it
        }
    }

    private fun roundingOffNumbers(number: Float, format: String = "#.00"):Float{
        val decimalFormat = DecimalFormat(format)
        return decimalFormat.format(number).toFloat()
    }


}



